home *** CD-ROM | disk | FTP | other *** search
/ Aminet 1 (Walnut Creek) / Aminet - June 1993 [Walnut Creek].iso / aminet / util / gnu / emacs_src_18_58.lha / emacs-18.58 / lisp / rmail.el < prev    next >
Lisp/Scheme  |  1992-02-21  |  50KB  |  1,415 lines

  1. ;; "RMAIL" mail reader for Emacs.
  2. ;; Copyright (C) 1985, 1986, 1987, 1988, 1990 Free Software Foundation, Inc.
  3.  
  4. ;; This file is part of GNU Emacs.
  5.  
  6. ;; GNU Emacs is free software; you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation; either version 1, or (at your option)
  9. ;; any later version.
  10.  
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. ;; GNU General Public License for more details.
  15.  
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs; see the file COPYING.  If not, write to
  18. ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  
  20.  
  21. ;; Souped up by shane@mit-ajax based on ideas of rlk@athena.mit.edu
  22. ;;   New features include attribute and keyword support, message
  23. ;;   selection by dispatch table, summary by attributes and keywords,
  24. ;;   expunging by dispatch table, sticky options for file commands.
  25.  
  26. (require 'mail-utils)
  27. (provide 'rmail)
  28.  
  29. ; these variables now declared in loaddefs or paths.el
  30. ;(defvar rmail-spool-directory "/usr/spool/mail/"
  31. ;  "This is the name of the directory used by the system mailer for\n\
  32. ;delivering new mail.  It's name should end with a slash.")
  33. ;(defvar rmail-dont-reply-to-names
  34. ;  nil
  35. ;  "*A regexp specifying names to prune of reply to messages.
  36. ;nil means dont reply to yourself.")
  37. ;(defvar rmail-ignored-headers
  38. ;   "^via:\\|^mail-from:\\|^origin:\\|^status:\\|^received:\\|^message-id:\\|^summary-line:"
  39. ;   "*Gubbish headers one would rather not see.")
  40. ;(defvar rmail-file-name
  41. ;  (expand-file-name "~/RMAIL")
  42. ;  "")
  43. ;
  44. ;(defvar rmail-delete-after-output nil
  45. ;  "*Non-nil means automatically delete a message that is copied to a file.")
  46. ;
  47. ;(defvar rmail-primary-inbox-list 
  48. ;  '("/usr/spool/mail/$USER" "~/mbox")
  49. ; "")
  50.  
  51. ;; these may be altered by site-init.el to match the format of mmdf files
  52. ;;  delimitation used on a given host (delim1 and delim2 from the config
  53. ;;  files)
  54.  
  55. (defvar mmdf-delim1 "^\001\001\001\001\n"
  56.   "Regexp marking the start of an mmdf message")
  57. (defvar mmdf-delim2 "^\001\001\001\001\n"
  58.   "Regexp marking the end of an mmdf message")
  59.  
  60. (defvar rmail-message-filter nil
  61.   "If non nil, is a filter function for new headers in RMAIL.
  62. Called with region narrowed to unformatted header.")
  63.  
  64. (defvar rmail-mode-map nil)
  65.  
  66. ;; Message counters and markers.  Deleted flags.
  67.  
  68. (defvar rmail-current-message nil)
  69. (defvar rmail-total-messages nil)
  70. (defvar rmail-message-vector nil)
  71. (defvar rmail-deleted-vector nil)
  72.  
  73. ;; These are used by autoloaded rmail-summary.
  74.  
  75. (defvar rmail-summary-buffer nil)
  76. (defvar rmail-summary-vector nil)
  77.  
  78. ;; `Sticky' default variables.
  79.  
  80. ;; Last individual label specified to a or k.
  81. (defvar rmail-last-label nil)
  82. ;; Last set of labels specified to C-M-n or C-M-p or C-M-l.
  83. (defvar rmail-last-multi-labels nil)
  84. (defvar rmail-last-file nil)
  85. (defvar rmail-last-rmail-file nil)
  86.  
  87. ;;;; *** Rmail Mode ***
  88.  
  89. (defun rmail (&optional file-name-arg)
  90.   "Read and edit incoming mail.
  91. Moves messages into file named by  rmail-file-name  (a babyl format file)
  92.  and edits that file in RMAIL Mode.
  93. Type \\[describe-mode] once editing that file, for a list of RMAIL commands.
  94.  
  95. May be called with filename as argument;
  96. then performs rmail editing on that file,
  97. but does not copy any new mail into the file."
  98.   (interactive (if current-prefix-arg
  99.            (list (read-file-name "Run rmail on RMAIL file: "
  100.                      nil nil t))))
  101.   (or rmail-last-file
  102.       (setq rmail-last-file (expand-file-name "~/xmail")))
  103.   (or rmail-last-rmail-file
  104.       (setq rmail-last-rmail-file (expand-file-name "~/XMAIL")))
  105.   (let* ((file-name (expand-file-name (or file-name-arg rmail-file-name)))
  106.      (existed (get-file-buffer file-name))
  107.      ;; Don't be confused by apparent local-variables spec
  108.      ;; in the last message in the RMAIL file.
  109.      (inhibit-local-variables t))
  110.     ;; Like find-file, but in the case where a buffer existed
  111.     ;; and the file was reverted, recompute the message-data.
  112.     (if (and existed (not (verify-visited-file-modtime existed)))
  113.     (progn
  114.       (find-file file-name)
  115.       (if (and (verify-visited-file-modtime existed)
  116.            (eq major-mode 'rmail-mode))
  117.           (progn (rmail-forget-messages)
  118.              (rmail-set-message-counters))))
  119.       (find-file file-name))
  120.     (if (eq major-mode 'rmail-edit-mode)
  121.     (error "exit rmail-edit-mode before getting new mail"))
  122.     (if (and existed (eq major-mode 'rmail-mode))
  123.     nil
  124.       (rmail-mode)
  125.       ;; Provide default set of inboxes for primary mail file ~/RMAIL.
  126.       (and (null rmail-inbox-list)
  127.        (null file-name-arg)
  128.        (setq rmail-inbox-list
  129.          (or rmail-primary-inbox-list
  130.              (list "~/mbox"
  131.                (concat rmail-spool-directory
  132.                    (or (getenv "LOGNAME")
  133.                        (getenv "USER")
  134.                        (user-login-name)))))))
  135.       ;; Convert all or part to Babyl file if possible.
  136.       (rmail-convert-file)
  137.       (goto-char (point-max))
  138.       (if (null rmail-inbox-list)
  139.       (progn
  140.         (rmail-set-message-counters)
  141.         (rmail-show-message))))
  142.     (rmail-get-new-mail)))
  143.  
  144. (defun rmail-convert-file ()
  145.   (let (convert)
  146.     (widen)
  147.     (goto-char (point-min))
  148.     ;; If file doesn't start like a Babyl file,
  149.     ;; convert it to one, by adding a header and converting each message.
  150.     (cond ((looking-at "BABYL OPTIONS:"))
  151.       ((looking-at "Version: 5\n")
  152.        ;; Losing babyl file made by old version of Rmail.
  153.        ;; Just fix the babyl file header; don't make a new one,
  154.        ;; so we don't lose the Labels: file attribute, etc.
  155.        (let ((buffer-read-only nil))
  156.          (insert "BABYL OPTIONS:\n")))
  157.       (t
  158.        (setq convert t)
  159.        (rmail-insert-rmail-file-header)))
  160.     ;; If file was not a Babyl file or if there are
  161.     ;; Unix format messages added at the end,
  162.     ;; convert file as necessary.
  163.     (if (or convert
  164.         (progn (goto-char (point-max))
  165.            (search-backward "\^_")
  166.            (forward-char 1)
  167.            (looking-at "\n*From ")))
  168.     (let ((buffer-read-only nil))
  169.       (message "Converting to Babyl format...")
  170.       (narrow-to-region (point) (point-max))
  171.       (rmail-convert-to-babyl-format)
  172.       (message "Converting to Babyl format...done")))))
  173.  
  174. (defun rmail-insert-rmail-file-header ()
  175.   (let ((buffer-read-only nil))
  176.     (insert "BABYL OPTIONS:
  177. Version: 5
  178. Labels:
  179. Note:   This is the header of an rmail file.
  180. Note:   If you are seeing it in rmail,
  181. Note:    it means the file has no messages in it.\n\^_")))
  182.  
  183. (if rmail-mode-map
  184.     nil
  185.   (setq rmail-mode-map (make-keymap))
  186.   (suppress-keymap rmail-mode-map)
  187.   (define-key rmail-mode-map "." 'rmail-beginning-of-message)
  188.   (define-key rmail-mode-map " " 'scroll-up)
  189.   (define-key rmail-mode-map "\177" 'scroll-down)
  190.   (define-key rmail-mode-map "n" 'rmail-next-undeleted-message)
  191.   (define-key rmail-mode-map "p" 'rmail-previous-undeleted-message)
  192.   (define-key rmail-mode-map "\en" 'rmail-next-message)
  193.   (define-key rmail-mode-map "\ep" 'rmail-previous-message)
  194.   (define-key rmail-mode-map "\e\C-n" 'rmail-next-labeled-message)
  195.   (define-key rmail-mode-map "\e\C-p" 'rmail-previous-labeled-message)
  196.   (define-key rmail-mode-map "a" 'rmail-add-label)
  197.   (define-key rmail-mode-map "k" 'rmail-kill-label)
  198.   (define-key rmail-mode-map "d" 'rmail-delete-forward)
  199.   (define-key rmail-mode-map "u" 'rmail-undelete-previous-message)
  200.   (define-key rmail-mode-map "e" 'rmail-expunge)
  201.   (define-key rmail-mode-map "x" 'rmail-expunge)
  202.   (define-key rmail-mode-map "s" 'rmail-expunge-and-save)
  203.   (define-key rmail-mode-map "g" 'rmail-get-new-mail)
  204.   (define-key rmail-mode-map "h" 'rmail-summary)
  205.   (define-key rmail-mode-map "\e\C-h" 'rmail-summary)
  206.   (define-key rmail-mode-map "l" 'rmail-summary-by-labels)
  207.   (define-key rmail-mode-map "\e\C-l" 'rmail-summary-by-labels)
  208.   (define-key rmail-mode-map "\e\C-r" 'rmail-summary-by-recipients)
  209.   (define-key rmail-mode-map "t" 'rmail-toggle-header)
  210.   (define-key rmail-mode-map "m" 'rmail-mail)
  211.   (define-key rmail-mode-map "r" 'rmail-reply)
  212.   (define-key rmail-mode-map "c" 'rmail-continue)
  213.   (define-key rmail-mode-map "f" 'rmail-forward)
  214.   (define-key rmail-mode-map "\es" 'rmail-search)
  215.   (define-key rmail-mode-map "j" 'rmail-show-message)
  216.   (define-key rmail-mode-map "o" 'rmail-output-to-rmail-file)
  217.   (define-key rmail-mode-map "\C-o" 'rmail-output)
  218.   (define-key rmail-mode-map "i" 'rmail-input)
  219.   (define-key rmail-mode-map "q" 'rmail-quit)
  220.   (define-key rmail-mode-map ">" 'rmail-last-message)
  221.   (define-key rmail-mode-map "?" 'describe-mode)
  222.   (define-key rmail-mode-map "w" 'rmail-edit-current-message)
  223.   (define-key rmail-mode-map "\C-d" 'rmail-delete-backward))
  224.  
  225. ;; Rmail mode is suitable only for specially formatted data.
  226. (put 'rmail-mode 'mode-class 'special)
  227.  
  228. (defun rmail-mode ()
  229.   "Rmail Mode is used by \\[rmail] for editing Rmail files.
  230. All normal editing commands are turned off.
  231. Instead, these commands are available:
  232.  
  233. .    Move point to front of this message (same as \\[beginning-of-buffer]).
  234. SPC    Scroll to next screen of this message.
  235. DEL    Scroll to previous screen of this message.
  236. n    Move to Next non-deleted message.
  237. p    Move to Previous non-deleted message.
  238. M-n    Move to Next message whether deleted or not.
  239. M-p    Move to Previous message whether deleted or not.
  240. >    Move to the last message in Rmail file.
  241. j    Jump to message specified by numeric position in file.
  242. M-s    Search for string and show message it is found in.
  243. d    Delete this message, move to next nondeleted.
  244. C-d    Delete this message, move to previous nondeleted.
  245. u    Undelete message.  Tries current message, then earlier messages
  246.     till a deleted message is found.
  247. e    Expunge deleted messages.
  248. s    Expunge and save the file.
  249. q       Quit Rmail: expunge, save, then switch to another buffer.
  250. C-x C-s Save without expunging.
  251. g    Move new mail from system spool directory or mbox into this file.
  252. m    Mail a message (same as \\[mail-other-window]).
  253. c    Continue composing outgoing message started before.
  254. r    Reply to this message.  Like m but initializes some fields.
  255. f    Forward this message to another user.
  256. o       Output this message to an Rmail file (append it).
  257. C-o    Output this message to a Unix-format mail file (append it).
  258. i    Input Rmail file.  Run Rmail on that file.
  259. a    Add label to message.  It will be displayed in the mode line.
  260. k    Kill label.  Remove a label from current message.
  261. C-M-n   Move to Next message with specified label
  262.           (label defaults to last one specified).
  263.           Standard labels: filed, unseen, answered, forwarded, deleted.
  264.           Any other label is present only if you add it with `a'.
  265. C-M-p   Move to Previous message with specified label
  266. C-M-h    Show headers buffer, with a one line summary of each message.
  267. C-M-l    Like h only just messages with particular label(s) are summarized.
  268. C-M-r   Like h only just messages with particular recipient(s) are summarized.
  269. t    Toggle header, show Rmail header if unformatted or vice versa.
  270. w    Edit the current message.  C-c C-c to return to Rmail."
  271.   (interactive)
  272.   (kill-all-local-variables)
  273.   (rmail-mode-1)
  274.   (rmail-variables)
  275.   (run-hooks 'rmail-mode-hook))
  276.  
  277. (defun rmail-mode-1 ()
  278.   (setq major-mode 'rmail-mode)
  279.   (setq mode-name "RMAIL")
  280.   (setq buffer-read-only t)
  281.   ;; No need to auto save RMAIL files.
  282.   (setq buffer-auto-save-file-name nil)
  283.   (if (boundp 'mode-line-modified)
  284.       (setq mode-line-modified "--- ")
  285.     (setq mode-line-format
  286.       (cons "--- " (cdr (default-value 'mode-line-format)))))
  287.   (use-local-map rmail-mode-map)
  288.   (set-syntax-table text-mode-syntax-table)
  289.   (setq local-abbrev-table text-mode-abbrev-table))
  290.  
  291. (defun rmail-variables ()
  292.   (make-local-variable 'revert-buffer-function)
  293.   (setq revert-buffer-function 'rmail-revert)
  294.   (make-local-variable 'rmail-last-label)
  295.   (make-local-variable 'rmail-deleted-vector)
  296.   (make-local-variable 'rmail-keywords)
  297.   (make-local-variable 'rmail-summary-buffer)
  298.   (make-local-variable 'rmail-summary-vector)
  299.   (make-local-variable 'rmail-current-message)
  300.   (make-local-variable 'rmail-total-messages)
  301.   (make-local-variable 'require-final-newline)
  302.   (setq require-final-newline nil)
  303.   (make-local-variable 'version-control)
  304.   (setq version-control 'never)
  305.   (make-local-variable 'file-precious-flag)
  306.   (setq file-precious-flag t)
  307.   (make-local-variable 'rmail-message-vector)
  308.   (make-local-variable 'rmail-last-file)
  309.   (make-local-variable 'rmail-inbox-list)
  310.   (setq rmail-inbox-list (rmail-parse-file-inboxes))
  311.   (make-local-variable 'rmail-keywords)
  312.   ;; this gets generated as needed
  313.   (setq rmail-keywords nil))
  314.  
  315. ;; Handle M-x revert-buffer done in an rmail-mode buffer.
  316. (defun rmail-revert (arg noconfirm)
  317.   (let (revert-buffer-function)
  318.     ;; Call our caller again, but this time it does the default thing.
  319.     (if (revert-buffer arg noconfirm)
  320.     ;; If the user said "yes", and we changed something,
  321.     ;; reparse the messages.
  322.     (progn
  323.       (rmail-convert-file)
  324.       (goto-char (point-max))
  325.       (rmail-set-message-counters)
  326.       (rmail-show-message)))))
  327.  
  328. ;; Return a list of files from this buffer's Mail: option.
  329. ;; Does not assume that messages have been parsed.
  330. ;; Just returns nil if buffer does not look like Babyl format.
  331. (defun rmail-parse-file-inboxes ()
  332.   (save-excursion
  333.     (save-restriction
  334.       (widen)
  335.       (goto-char 1)
  336.       (cond ((looking-at "BABYL OPTIONS:")
  337.          (search-forward "\^_" nil 'move)
  338.          (narrow-to-region 1 (point))
  339.          (goto-char 1)
  340.          (if (search-forward "\nMail:" nil t)
  341.          (progn
  342.            (narrow-to-region (point) (progn (end-of-line) (point)))
  343.            (goto-char (point-min))
  344.            (mail-parse-comma-list))))))))
  345.  
  346. (defun rmail-expunge-and-save ()
  347.   "Expunge and save RMAIL file."
  348.   (interactive)
  349.   (rmail-expunge)
  350.   (save-buffer))
  351.  
  352. (defun rmail-quit ()
  353.   "Quit out of RMAIL."
  354.   (interactive)
  355.   (rmail-expunge-and-save)
  356.   ;; Don't switch to the summary buffer even if it was recently visible.
  357.   (if rmail-summary-buffer
  358.       (bury-buffer rmail-summary-buffer))
  359.   (let ((obuf (current-buffer)))
  360.     (switch-to-buffer (other-buffer))
  361.     (bury-buffer obuf)))
  362.  
  363. (defun rmail-input (filename)
  364.   "Run RMAIL on file FILENAME."
  365.   (interactive "FRun rmail on RMAIL file: ")
  366.   (rmail filename))
  367.  
  368.  
  369. ;;;; *** Rmail input ***
  370.  
  371. ;; RLK feature not added in this version:
  372. ;; argument specifies inbox file or files in various ways.
  373.  
  374. (defun rmail-get-new-mail (&optional file-name)
  375.   "Move any new mail from this RMAIL file's inbox files.
  376. The inbox files can be specified with the file's Mail: option.
  377. The variable rmail-primary-inbox-list specifies the inboxes for
  378. your primary RMAIL file if it has no Mail: option.
  379. These are normally your ~/mbox and your /usr/spool/mail/$USER.
  380.  
  381. You can also specify the file to get new mail from.  In this
  382. case, the file of new mail is not changed or deleted.
  383. Noninteractively, you can pass the inbox file name as an argument.
  384. Interactively, a prefix argument causes us to read a file name
  385. and use that file as the inbox."
  386.   (interactive
  387.    (list (if current-prefix-arg
  388.          (read-file-name "Get new mail from file: "))))
  389.   (or (verify-visited-file-modtime (current-buffer))
  390.       (progn
  391.     (find-file (buffer-file-name))
  392.     (if (verify-visited-file-modtime (current-buffer))
  393.         (rmail-forget-messages))))
  394.   (rmail-maybe-set-message-counters)
  395.   (widen)
  396.   ;; Get rid of all undo records for this buffer.
  397.   (or (eq buffer-undo-list t)
  398.       (setq buffer-undo-list nil))
  399.   (unwind-protect
  400.       (let ((opoint (point))
  401.         (new-messages 0)
  402.         (delete-files ())
  403.         ;; If buffer has not changed yet, and has not been saved yet,
  404.         ;; don't replace the old backup file now.
  405.         (make-backup-files (and make-backup-files (buffer-modified-p)))
  406.         (buffer-read-only nil)
  407.         ;; Don't make undo records for what we do in getting mail.
  408.         (buffer-undo-list t))
  409.     (goto-char (point-max))
  410.     (skip-chars-backward " \t\n")    ; just in case of brain damage
  411.     (delete-region (point) (point-max))    ; caused by require-final-newline
  412.     (save-excursion
  413.       (save-restriction
  414.         (narrow-to-region (point) (point))
  415.         ;; Read in the contents of the inbox files,
  416.         ;; renaming them as necessary,
  417.         ;; and adding to the list of files to delete eventually.
  418.         (if file-name
  419.         (rmail-insert-inbox-text (list file-name) nil)
  420.           (setq delete-files (rmail-insert-inbox-text rmail-inbox-list t)))
  421.         ;; Scan the new text and convert each message to babyl format.
  422.         (goto-char (point-min))
  423.         (save-excursion
  424.           (setq new-messages (rmail-convert-to-babyl-format)))
  425.         (or (zerop new-messages)
  426.         (let (success)
  427.           (widen)
  428.           (search-backward "\^_")
  429.           (narrow-to-region (point) (point-max))
  430.           (goto-char (1+ (point-min)))
  431.           (rmail-count-new-messages)
  432.           (save-buffer)))
  433.         ;; Delete the old files, now that babyl file is saved.
  434.         (while delete-files
  435.           (condition-case ()
  436.           (delete-file (car delete-files))
  437.         (file-error nil))
  438.           (setq delete-files (cdr delete-files)))))
  439.     (if (= new-messages 0)
  440.         (progn (goto-char opoint)
  441.            (if (or file-name rmail-inbox-list)
  442.                (message "(No new mail has arrived)")))
  443.         (message "%d new message%s read"
  444.              new-messages (if (= 1 new-messages) "" "s"))))
  445.     ;; Don't leave the buffer screwed up if we get a disk-full error.
  446.     (rmail-show-message)))
  447.  
  448. (defun rmail-insert-inbox-text (files renamep)
  449.   (let (file tofile delete-files movemail popmail)
  450.     (while files
  451.       (setq file (expand-file-name (substitute-in-file-name (car files)))
  452.         ;;>> un*x specific <<
  453.         tofile (concat file "~"))
  454.       ;; If getting from mail spool directory,
  455.       ;; use movemail to move rather than renaming.
  456.       (setq movemail (equal (file-name-directory file) rmail-spool-directory))
  457.       (setq popmail (string-match "^po:" (file-name-nondirectory file)))
  458.       (if popmail (setq file (file-name-nondirectory file)
  459.             renamep t))
  460.       (if movemail
  461.       (progn
  462.         (setq tofile (expand-file-name
  463.                ".newmail"
  464.                ;; Use the directory of this rmail file
  465.                ;; because it's a nuisance to use the homedir
  466.                ;; if that is on a full disk and this rmail
  467.                ;; file isn't.
  468.                (file-name-directory
  469.                  (expand-file-name buffer-file-name))))
  470.         ;; On some systems, /usr/spool/mail/foo is a directory
  471.         ;; and the actual inbox is /usr/spool/mail/foo/foo.
  472.         (if (file-directory-p file)
  473.         (setq file (expand-file-name (or (getenv "LOGNAME")
  474.                          (getenv "USER")
  475.                          (user-login-name))
  476.                          file)))))
  477.       (if popmail
  478.       (message "Getting mail from post office ...")
  479.     (if (or (file-exists-p tofile) (file-exists-p file))
  480.         (message "Getting mail from %s..." file)))
  481.       ;; Set TOFILE if have not already done so, and
  482.       ;; rename or copy the file FILE to TOFILE if and as appropriate.
  483.       (cond ((not renamep)
  484.          (setq tofile file))
  485.         ((or (file-exists-p tofile) (and (not popmail)
  486.                          (not (file-exists-p file))))
  487.          nil)
  488.         ((and (not movemail) (not popmail))
  489.          (rename-file file tofile nil))
  490.         (t
  491.          (let ((errors nil))
  492.            (unwind-protect
  493.            (save-excursion
  494.              (setq errors (generate-new-buffer " *rmail loss*"))
  495.              (buffer-flush-undo errors)
  496.              (call-process
  497.                (expand-file-name "movemail" exec-directory)
  498.                nil errors nil file tofile)
  499.              (if (not (buffer-modified-p errors))
  500.              ;; No output => movemail won
  501.              nil
  502.                (set-buffer errors)
  503.                (subst-char-in-region (point-min) (point-max)
  504.                          ?\n ?\  )
  505.                (goto-char (point-max))
  506.                (skip-chars-backward " \t")
  507.                (delete-region (point) (point-max))
  508.                (goto-char (point-min))
  509.                (if (looking-at "movemail: ")
  510.                (delete-region (point-min) (match-end 0)))
  511.                (beep t)
  512.                (message (concat "movemail: "
  513.                     (buffer-substring (point-min)
  514.                               (point-max))))
  515.                (sit-for 3)
  516.                nil))
  517.          (if errors (kill-buffer errors))))))
  518.       ;; At this point, TOFILE contains the name to read:
  519.       ;; Either the alternate name (if we renamed)
  520.       ;; or the actual inbox (if not renaming).
  521.       (if (file-exists-p tofile)
  522.       (let ((omax (point-max)))
  523.         (goto-char (point-max))
  524.         (insert-file-contents tofile)
  525.         (goto-char (point-max))
  526.         (or (= (preceding-char) ?\n)
  527.         (= opoint (point-max))
  528.         (insert ?\n))
  529.         (setq delete-files (cons tofile delete-files))))
  530.       (message "")
  531.       (setq files (cdr files)))
  532.     delete-files))
  533.  
  534. ;; the  rmail-break-forwarded-messages  feature is not implemented
  535. (defun rmail-convert-to-babyl-format ()
  536.   (let ((count 0) start
  537.     (case-fold-search nil))
  538.     (goto-char (point-min))
  539.     (save-restriction
  540.       (while (not (eobp))
  541.     (cond ((looking-at "BABYL OPTIONS:");Babyl header
  542.            (search-forward "\n\^_")
  543.            (delete-region (point-min) (point)))
  544.           ;; Babyl format message
  545.           ((looking-at "\^L")
  546.            (or (search-forward "\n\^_" nil t)
  547.            (progn
  548.              (message "Invalid Babyl format in inbox!")
  549.              (sit-for 1)
  550.              (goto-char (point-max))))
  551.            (setq count (1+ count))
  552.            ;; Make sure there is no extra white space after the ^_
  553.            ;; at the end of the message.
  554.            ;; Narrowing will make sure that whatever follows the junk
  555.            ;; will be treated properly.
  556.            (delete-region (point)
  557.                   (save-excursion
  558.                 (skip-chars-forward " \t\n")
  559.                 (point)))
  560.            (narrow-to-region (point) (point-max)))
  561.           ;;*** MMDF format
  562.           ((let ((case-fold-search t))
  563.          (looking-at mmdf-delim1))
  564.            (let ((case-fold-search t))
  565.          (replace-match "\^L\n0, unseen,,\n*** EOOH ***\n")
  566.          (setq start (point))
  567.          (re-search-forward mmdf-delim2 nil t)
  568.          (replace-match "\^_"))
  569.            (save-excursion
  570.          (save-restriction
  571.            (narrow-to-region start (1- (point)))
  572.            (goto-char (point-min))
  573.            (while (search-forward "\n\^_" nil t) ; single char "\^_"
  574.              (replace-match "\n^_")))) ; 2 chars: "^" and "_"
  575.            (narrow-to-region (point) (point-max))
  576.            (setq count (1+ count)))
  577.           ;;*** Mail format
  578.           ((looking-at "^From ")
  579.            (setq start (point))
  580.            (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
  581.            (rmail-nuke-pinhead-header)
  582.            ;; If this message has a Content-Length field,
  583.            ;; skip to the end of the contents.
  584.            (let* ((header-end (save-excursion
  585.                     (and (re-search-forward "\n\n" nil t)
  586.                      (point))))
  587.               (case-fold-search t)
  588.               (size
  589.                ;; Get the numeric value from the Content-Length field.
  590.                (save-excursion
  591.              ;; Back up to end of prev line,
  592.              ;; in case the Content-Length field comes first.
  593.              (forward-char -1)
  594.              (and (search-forward "\ncontent-length: "
  595.                           header-end t)
  596.                   (let ((beg (point))
  597.                     (eol (progn (end-of-line) (point))))
  598.                 (read (buffer-substring beg eol)))))))
  599.          (if size
  600.              (goto-char (+ header-end size))))
  601.              
  602.            (if (re-search-forward
  603.             (concat "^[\^_]?\\("
  604.                 "From [^ \n]*\\(\\|\".*\"[^ \n]*\\)  ?[^ \n]* [^ \n]* *"
  605.                 "[0-9]* [0-9:]*\\( ?[A-Z]?[A-Z][A-Z]T\\| ?[-+]?[0-9][0-9][0-9][0-9]\\|\\) " ; EDT, -0500
  606.                 "19[0-9]* *\\(remote from .*\\)?$\\|"
  607.                 mmdf-delim1 "\\|"
  608.                 "^BABYL OPTIONS:\\|"
  609.                 "\^L\n[01],\\)") nil t)
  610.            (goto-char (match-beginning 1))
  611.          (goto-char (point-max)))
  612.            (setq count (1+ count))
  613.            (save-excursion
  614.          (save-restriction
  615.            (narrow-to-region start (point))
  616.            (goto-char (point-min))
  617.            (while (search-forward "\n\^_" nil t); single char
  618.              (replace-match "\n^_")))); 2 chars: "^" and "_"
  619.            (insert ?\^_)
  620.            (narrow-to-region (point) (point-max)))
  621.           ;;
  622.           ;;This is a kludge, in case we're wrong about mmdf not
  623.           ;;allowing anything in between.  If it loses, we'll have
  624.           ;;to look for something else
  625.           (t (delete-char 1)))))
  626.     count))
  627.  
  628. (defun rmail-nuke-pinhead-header ()
  629.   (save-excursion
  630.     (save-restriction
  631.       (let ((start (point))
  632.           (end (progn
  633.            (condition-case ()
  634.                (search-forward "\n\n")
  635.              (error
  636.               (goto-char (point-max))
  637.               (insert "\n\n")))
  638.            (point)))
  639.         has-from has-date)
  640.     (narrow-to-region start end)
  641.     (let ((case-fold-search t))
  642.       (goto-char start)
  643.       (setq has-from (search-forward "\nFrom:" nil t))
  644.       (goto-char start)
  645.       (setq has-date (and (search-forward "\nDate:" nil t) (point)))
  646.       (goto-char start))
  647.     (let ((case-fold-search nil))
  648.       (if (re-search-forward
  649.            "^From \\([^ ]*\\(\\|\".*\"[^ ]*\\)\\)  ?\\([^ ]*\\) \\([^ ]*\\) *\\([0-9]*\\) \\([0-9:]*\\)\\( ?[A-Z]?[A-Z][A-Z]T\\| ?[-+]?[0-9][0-9][0-9][0-9]\\|\\) 19\\([0-9]*\\) *\\(remote from [^\n]*\\)?\n" nil t)
  650.           (replace-match
  651.         (concat
  652.           ;; Keep and reformat the date if we don't
  653.           ;;  have a Date: field.
  654.           (if has-date
  655.               ""
  656.             ;; If no time zone specified, assume est.
  657.             (if (= (match-beginning 7) (match-end 7))
  658.             "Date: \\3, \\5 \\4 \\8 \\6 EST\n"
  659.             "Date: \\3, \\5 \\4 \\8 \\6\\7\n"))
  660.           ;; Keep and reformat the sender if we don't
  661.           ;; have a From: field.
  662.           (if has-from
  663.               ""
  664.             "From: \\1\n")))))))))
  665.  
  666. ;;;; *** Rmail Message Formatting and Header Manipulation ***
  667.  
  668. (defun rmail-reformat-message (beg end)
  669.   (goto-char beg)
  670.   (forward-line 1)
  671.   (if (/= (following-char) ?0)
  672.       (error "Bad format in RMAIL file."))
  673.   (let ((buffer-read-only nil)
  674.     (delta (- (buffer-size) end)))
  675.     (delete-char 1)
  676.     (insert ?1)
  677.     (forward-line 1)
  678.     (if (looking-at "Summary-line: ")
  679.     (forward-line 1))
  680.     (if (looking-at "\\*\\*\\* EOOH \\*\\*\\*\n")
  681.     (delete-region (point)
  682.                (progn (forward-line 1) (point))))
  683.     (let ((str (buffer-substring (point)
  684.                  (save-excursion (search-forward "\n\n" end 'move)
  685.                          (point)))))
  686.       (insert str "*** EOOH ***\n")
  687.       (narrow-to-region (point) (- (buffer-size) delta)))
  688.     (goto-char (point-min))
  689.     (if rmail-ignored-headers (rmail-clear-headers))
  690.     (if rmail-message-filter (funcall rmail-message-filter))))
  691.  
  692. (defun rmail-clear-headers ()
  693.   (if (search-forward "\n\n" nil t)
  694.       (save-restriction
  695.         (narrow-to-region (point-min) (point))
  696.     (let ((buffer-read-only nil))
  697.       (while (let ((case-fold-search t))
  698.            (goto-char (point-min))
  699.            (re-search-forward rmail-ignored-headers nil t))
  700.         (beginning-of-line)
  701.         (delete-region (point)
  702.                (progn (re-search-forward "\n[^ \t]")
  703.                   (forward-char -1)
  704.                   (point))))))))
  705.  
  706. (defun rmail-toggle-header ()
  707.   "Show original message header if pruned header currently shown, or vice versa."
  708.   (interactive)
  709.   (rmail-maybe-set-message-counters)
  710.   (narrow-to-region (rmail-msgbeg rmail-current-message) (point-max))
  711.   (let ((buffer-read-only nil))
  712.     (goto-char (point-min))
  713.     (forward-line 1)
  714.     (if (= (following-char) ?1)
  715.     (progn (delete-char 1)
  716.            (insert ?0)
  717.            (forward-line 1)
  718.            (if (looking-at "Summary-Line:")
  719.            (forward-line 1))
  720.            (insert "*** EOOH ***\n")
  721.            (forward-char -1)
  722.            (search-forward "\n*** EOOH ***\n")
  723.            (forward-line -1)
  724.            (let ((temp (point)))
  725.          (and (search-forward "\n\n" nil t)
  726.               (delete-region temp (point))))
  727.            (goto-char (point-min))
  728.            (search-forward "\n*** EOOH ***\n")
  729.            (narrow-to-region (point) (point-max)))
  730.       (rmail-reformat-message (point-min) (point-max)))))
  731.  
  732. ;;;; *** Rmail Attributes and Keywords ***
  733.  
  734. ;; Make a string describing current message's attributes and keywords
  735. ;; and set it up as the name of a minor mode
  736. ;; so it will appear in the mode line.
  737. (defun rmail-display-labels ()
  738.   (let ((blurb "") (beg (point-min-marker)) (end (point-max-marker)))
  739.     (save-excursion
  740.       (unwind-protect
  741.       (progn
  742.         (widen)
  743.         (goto-char (rmail-msgbeg rmail-current-message))
  744.         (forward-line 1)
  745.         (if (looking-at "[01],")
  746.         (progn
  747.           (narrow-to-region (point) (progn (end-of-line) (point)))
  748.           ;; Truly valid BABYL format requires a space before each
  749.           ;; attribute or keyword name.  Put them in if missing.
  750.           (let (buffer-read-only)
  751.             (goto-char (point-min))
  752.             (while (search-forward "," nil t)
  753.               (or (looking-at "[ ,]") (eobp)
  754.               (insert " "))))
  755.           (goto-char (point-max))
  756.           (if (search-backward ",," nil 'move)
  757.               (progn
  758.             (if (> (point) (1+ (point-min)))
  759.                 (setq blurb (buffer-substring (+ 1 (point-min)) (point))))
  760.             (if (> (- (point-max) (point)) 2)
  761.                 (setq blurb
  762.                   (concat blurb
  763.                       ";"
  764.                       (buffer-substring (+ (point) 3)
  765.                                 (1- (point-max)))))))))))
  766.     (narrow-to-region beg end)
  767.     (set-marker beg nil)
  768.     (set-marker end nil)))
  769.     (while (string-match " +," blurb)
  770.       (setq blurb (concat (substring blurb 0 (match-beginning 0)) ","
  771.               (substring blurb (match-end 0)))))
  772.     (while (string-match ", +" blurb)
  773.       (setq blurb (concat (substring blurb 0 (match-beginning 0)) ","
  774.               (substring blurb (match-end 0)))))
  775.     (setq mode-line-process
  776.       (concat " " rmail-current-message "/" rmail-total-messages
  777.           blurb))))
  778.  
  779. ;; Turn an attribute of the current message on or off according to STATE.
  780. ;; ATTR is the name of the attribute, as a string.
  781. (defun rmail-set-attribute (attr state)
  782.   (let ((omax (- (buffer-size) (point-max)))
  783.     (omin (- (buffer-size) (point-min)))
  784.     (buffer-read-only nil))
  785.     (unwind-protect
  786.     (save-excursion
  787.       (widen)
  788.       (goto-char (+ 3 (rmail-msgbeg rmail-current-message)))
  789.       (let ((curstate (search-backward (concat ", " attr ",")
  790.                        (prog1 (point) (end-of-line)) t)))
  791.         (or (eq curstate (not (not state)))
  792.         (if curstate
  793.             (delete-region (point) (1- (match-end 0)))
  794.           (beginning-of-line)
  795.           (forward-char 2)
  796.           (insert " " attr ","))))
  797.       (if (string= attr "deleted")
  798.           (rmail-set-message-deleted-p rmail-current-message state)))
  799.       (narrow-to-region (max 1 (- (buffer-size) omin))
  800.             (- (buffer-size) omax))
  801.       (rmail-display-labels))))
  802.  
  803. ;; Return t if the attributes/keywords line of msg number MSG
  804. ;; contains a match for the regexp LABELS.
  805. (defun rmail-message-labels-p (msg labels)
  806.   (goto-char (rmail-msgbeg msg))
  807.   (forward-char 3)
  808.   (re-search-backward labels (prog1 (point) (end-of-line)) t))
  809.  
  810. ;;;; *** Rmail Message Selection And Support ***
  811.  
  812. (defun rmail-msgend (n)
  813.   (marker-position (aref rmail-message-vector (1+ n))))
  814.  
  815. (defun rmail-msgbeg (n)
  816.   (marker-position (aref rmail-message-vector n)))
  817.  
  818. (defun rmail-widen-to-current-msgbeg (function)
  819.   "Call FUNCTION with point at start of internal data of current message.
  820. Assumes that bounds were previously narrowed to display the message in Rmail.
  821. The bounds are widened enough to move point where desired,
  822. then narrowed again afterward.
  823. Assumes that the visible text of the message is not changed by FUNCTION."
  824.   (save-excursion
  825.     (let ((obeg (- (point-max) (point-min)))
  826.       (unwind-protect
  827.       (progn
  828.         (narrow-to-region (rmail-msgbeg rmail-current-message)
  829.                   (point-max))
  830.         (goto-char (point-min))
  831.         (funcall function))
  832.     (narrow-to-region (- (point-max) obeg) (point-max)))))))
  833.  
  834. (defun rmail-forget-messages ()
  835.   (unwind-protect
  836.       (if (vectorp rmail-message-vector)
  837.       (let* ((i 0)
  838.          (v rmail-message-vector)
  839.          (n (length v)))
  840.         (while (< i n)
  841.           (move-marker (aref v i)  nil)
  842.           (setq i (1+ i)))))
  843.     (setq rmail-message-vector nil)
  844.     (setq rmail-deleted-vector nil)))
  845.  
  846. (defun rmail-maybe-set-message-counters ()
  847.   (if (not (and rmail-deleted-vector
  848.         rmail-message-vector
  849.         rmail-current-message
  850.         rmail-total-messages))
  851.       (rmail-set-message-counters)))
  852.  
  853. (defun rmail-count-new-messages (&optional nomsg)
  854.   (let* ((case-fold-search nil)
  855.      (total-messages 0)
  856.      (messages-head nil)
  857.      (deleted-head nil))
  858.     (or nomsg (message "Counting new messages..."))
  859.     (goto-char (point-max))
  860.     ;; Put at the end of messages-head
  861.     ;; the entry for message N+1, which marks
  862.     ;; the end of message N.  (N = number of messages).
  863.     (search-backward "\^_")
  864.     (setq messages-head (list (point-marker)))
  865.     (rmail-set-message-counters-counter (point-min))
  866.     (setq rmail-current-message (1+ rmail-total-messages))
  867.     (setq rmail-total-messages
  868.       (+ rmail-total-messages total-messages))
  869.     (setq rmail-message-vector
  870.       (vconcat rmail-message-vector (cdr messages-head)))
  871.     (aset rmail-message-vector
  872.       rmail-current-message (car messages-head))
  873.     (setq rmail-deleted-vector
  874.       (concat rmail-deleted-vector deleted-head))
  875.     (setq rmail-summary-vector
  876.       (vconcat rmail-summary-vector (make-vector total-messages nil)))
  877.     (goto-char (point-min))
  878.     (or nomsg (message "Counting new messages...done (%d)" total-messages))))
  879.  
  880. (defun rmail-set-message-counters ()
  881.   (rmail-forget-messages)
  882.   (save-excursion
  883.     (save-restriction
  884.       (widen)
  885.       (let* ((point-save (point))
  886.          (total-messages 0)
  887.          (messages-after-point)
  888.          (case-fold-search nil)
  889.          (messages-head nil)
  890.          (deleted-head nil))
  891.     (message "Counting messages...")
  892.     (goto-char (point-max))
  893.     ;; Put at the end of messages-head
  894.     ;; the entry for message N+1, which marks
  895.     ;; the end of message N.  (N = number of messages).
  896.     (search-backward "\^_")
  897.     (setq messages-head (list (point-marker)))
  898.     (rmail-set-message-counters-counter (min (point) point-save))
  899.     (setq messages-after-point total-messages)
  900.     (rmail-set-message-counters-counter)
  901.     (setq rmail-total-messages total-messages)
  902.     (setq rmail-current-message
  903.           (min total-messages
  904.            (max 1 (- total-messages messages-after-point))))
  905.     (setq rmail-message-vector
  906.           (apply 'vector (cons (point-min-marker) messages-head))
  907.           rmail-deleted-vector (concat "D" deleted-head)
  908.           rmail-summary-vector (make-vector rmail-total-messages nil))
  909.     (message "Counting messages...done")))))
  910.     
  911. (defun rmail-set-message-counters-counter (&optional stop)
  912.   (while (search-backward "\^_\^L\n" stop t)
  913.     (setq messages-head (cons (point-marker) messages-head))
  914.     (save-excursion
  915.       (setq deleted-head
  916.         (cons (if (search-backward ", deleted,"
  917.                        (prog1 (point)
  918.                      (forward-line 2))
  919.                        t)
  920.               ?D ?\ )
  921.           deleted-head)))
  922.     (if (zerop (% (setq total-messages (1+ total-messages)) 20))
  923.     (message "Counting messages...%d" total-messages))))
  924.  
  925. (defun rmail-beginning-of-message ()
  926.   "Show current message starting from the beginning."
  927.   (interactive)
  928.   (rmail-show-message rmail-current-message))
  929.  
  930. (defun rmail-show-message (&optional n)
  931.   "Show message number N (prefix argument), counting from start of file."
  932.   (interactive "p")
  933.   (rmail-maybe-set-message-counters)
  934.   (widen)
  935.   (if (zerop rmail-total-messages)
  936.       (progn (narrow-to-region (point-min) (1- (point-max)))
  937.          (goto-char (point-min))
  938.          (setq mode-line-process nil))
  939.     (let (blurb)
  940.       (if (not n)
  941.       (setq n rmail-current-message)
  942.     (cond ((<= n 0)
  943.            (setq n 1
  944.              rmail-current-message 1
  945.              blurb "No previous message"))
  946.           ((> n rmail-total-messages)
  947.            (setq n rmail-total-messages
  948.              rmail-current-message rmail-total-messages
  949.              blurb "No following message"))
  950.           (t
  951.            (setq rmail-current-message n))))
  952.       (let ((beg (rmail-msgbeg n))
  953.         (end (rmail-msgend n)))
  954.     (goto-char beg)
  955.     (forward-line 1)
  956.     (if (= (following-char) ?0)
  957.         (progn
  958.           (rmail-reformat-message beg end)
  959.           (rmail-set-attribute "unseen" nil))
  960.       (search-forward "\n*** EOOH ***\n" end t)
  961.       (narrow-to-region (point) end))
  962.     (goto-char (point-min))
  963.     (rmail-display-labels)
  964.     (run-hooks 'rmail-show-message-hook)
  965.     (if blurb
  966.         (message blurb))))))
  967.  
  968. (defun rmail-next-message (n)
  969.   "Show following message whether deleted or not.
  970. With prefix argument N, moves forward N messages,
  971. or backward if N is negative."
  972.   (interactive "p")
  973.   (rmail-maybe-set-message-counters)
  974.   (rmail-show-message (+ rmail-current-message n)))
  975.  
  976. (defun rmail-previous-message (n)
  977.   "Show previous message whether deleted or not.
  978. With prefix argument N, moves backward N messages,
  979. or forward if N is negative."
  980.   (interactive "p")
  981.   (rmail-next-message (- n)))  
  982.  
  983. (defun rmail-next-undeleted-message (n)
  984.   "Show following non-deleted message.
  985. With prefix argument N, moves forward N non-deleted messages,
  986. or backward if N is negative."
  987.   (interactive "p")
  988.   (rmail-maybe-set-message-counters)
  989.   (let ((lastwin rmail-current-message)
  990.     (current rmail-current-message))
  991.     (while (and (> n 0) (< current rmail-total-messages))
  992.       (setq current (1+ current))
  993.       (if (not (rmail-message-deleted-p current))
  994.       (setq lastwin current n (1- n))))
  995.     (while (and (< n 0) (> current 1))
  996.       (setq current (1- current))
  997.       (if (not (rmail-message-deleted-p current))
  998.       (setq lastwin current n (1+ n))))
  999.     (if (/= lastwin rmail-current-message)
  1000.     (rmail-show-message lastwin))
  1001.     (if (< n 0)
  1002.     (message "No previous nondeleted message"))
  1003.     (if (> n 0)
  1004.     (message "No following nondeleted message"))))
  1005.  
  1006. (defun rmail-previous-undeleted-message (n)
  1007.   "Show previous non-deleted message.
  1008. With prefix argument N, moves backward N non-deleted messages,
  1009. or forward if N is negative."
  1010.   (interactive "p")
  1011.   (rmail-next-undeleted-message (- n)))
  1012.  
  1013. (defun rmail-last-message ()
  1014.   "Show last message in file."
  1015.   (interactive)
  1016.   (rmail-maybe-set-message-counters)
  1017.   (rmail-show-message rmail-total-messages))
  1018.  
  1019. (defun rmail-what-message ()
  1020.   (let ((where (point))
  1021.     (low 1)
  1022.     (high rmail-total-messages)
  1023.     (mid (/ rmail-total-messages 2)))
  1024.     (while (> (- high low) 1)
  1025.       (if (>= where (rmail-msgbeg mid))
  1026.       (setq low mid)
  1027.     (setq high mid))
  1028.       (setq mid (+ low (/ (- high low) 2))))
  1029.     (if (>= where (rmail-msgbeg high)) high low)))
  1030.  
  1031. (defvar rmail-search-last-regexp nil)
  1032. (defun rmail-search (regexp &optional reversep)
  1033.   "Show message containing next match for REGEXP.
  1034. Search in reverse (earlier messages) with non-nil 2nd arg REVERSEP.
  1035. Interactively, empty argument means use same regexp used last time,
  1036. and reverse search is specified by a negative numeric arg."
  1037.   (interactive
  1038.     (let* ((reversep (< (prefix-numeric-value current-prefix-arg) 0))
  1039.        (prompt (concat (if reversep "Reverse " "") "Rmail search (regexp): "))
  1040.        regexp)
  1041.       (if rmail-search-last-regexp
  1042.       (setq prompt (concat prompt
  1043.                    "(default "
  1044.                    rmail-search-last-regexp
  1045.                    ") ")))
  1046.       (setq regexp (read-string prompt))
  1047.       (cond ((not (equal regexp ""))
  1048.          (setq rmail-search-last-regexp regexp))
  1049.         ((not rmail-search-last-regexp)
  1050.          (error "No previous Rmail search string")))
  1051.       (list rmail-search-last-regexp reversep)))
  1052.   (message "%sRmail search for %s..."
  1053.        (if reversep "Reverse " "")
  1054.        regexp)
  1055.   (rmail-maybe-set-message-counters)
  1056.   (let ((omin (point-min))
  1057.     (omax (point-max))
  1058.     (opoint (point))
  1059.     win
  1060.     (msg rmail-current-message))
  1061.     (unwind-protect
  1062.     (progn
  1063.       (widen)
  1064.       ;; Check messages one by one, advancing message number up or down
  1065.       ;; but searching forward through each message.
  1066.       (if reversep
  1067.           (while (and (null win) (> msg 1))
  1068.         (goto-char (rmail-msgbeg (setq msg (1- msg))))
  1069.         (setq win (re-search-forward
  1070.                regexp (rmail-msgend msg) t)))
  1071.         (while (and (null win) (< msg rmail-total-messages))
  1072.           (goto-char (rmail-msgbeg (setq msg (1+ msg))))
  1073.           (setq win (re-search-forward regexp (rmail-msgend msg) t)))))
  1074.       (if win
  1075.       (progn
  1076.         ;; If this is a reverse search and we found a message,
  1077.         ;; search backward thru this message to position point.
  1078.         (if reversep
  1079.         (progn
  1080.           (goto-char (rmail-msgend msg))
  1081.           (re-search-backward
  1082.            regexp (rmail-msgbeg msg) t)))
  1083.         (setq win (point))
  1084.         (rmail-show-message msg)
  1085.         (message "%sRmail search for %s...done"
  1086.              (if reversep "Reverse " "")
  1087.              regexp)
  1088.         (goto-char win))
  1089.     (goto-char opoint)
  1090.     (narrow-to-region omin omax)
  1091.     (ding)
  1092.     (message "Searched failed: %s" regexp)))))
  1093.  
  1094. ;;;; *** Rmail Message Deletion Commands ***
  1095.  
  1096. (defun rmail-message-deleted-p (n)
  1097.   (= (aref rmail-deleted-vector n) ?D))
  1098.  
  1099. (defun rmail-set-message-deleted-p (n state)
  1100.   (aset rmail-deleted-vector n (if state ?D ?\ )))
  1101.  
  1102. (defun rmail-delete-message ()
  1103.   "Delete this message and stay on it."
  1104.   (interactive)
  1105.   (rmail-set-attribute "deleted" t))
  1106.  
  1107. (defun rmail-undelete-previous-message ()
  1108.   "Back up to deleted message, select it, and undelete it."
  1109.   (interactive)
  1110.   (let ((msg rmail-current-message))
  1111.     (while (and (> msg 0)
  1112.         (not (rmail-message-deleted-p msg)))
  1113.       (setq msg (1- msg)))
  1114.     (if (= msg 0)
  1115.     (error "No previous deleted message")
  1116.       (if (/= msg rmail-current-message)
  1117.       (rmail-show-message msg))
  1118.       (rmail-set-attribute "deleted" nil))))
  1119.  
  1120. (defun rmail-delete-forward (&optional backward)
  1121.   "Delete this message and move to next nondeleted one.
  1122. Deleted messages stay in the file until the \\[rmail-expunge] command is given.
  1123. With prefix argument, delete and move backward."
  1124.   (interactive "P")
  1125.   (rmail-set-attribute "deleted" t)
  1126.   (rmail-next-undeleted-message (if backward -1 1)))
  1127.  
  1128. (defun rmail-delete-backward ()
  1129.   "Delete this message and move to previous nondeleted one.
  1130. Deleted messages stay in the file until the \\[rmail-expunge] command is given."
  1131.   (interactive)
  1132.   (rmail-delete-forward t))
  1133.  
  1134. (defun rmail-expunge ()
  1135.   "Actually erase all deleted messages in the file."
  1136.   (interactive)
  1137.   (message "Expunging deleted messages...")
  1138.   ;; Discard any prior undo information.
  1139.   (or (eq buffer-undo-list t)
  1140.       (setq buffer-undo-list nil))
  1141.   (rmail-maybe-set-message-counters)
  1142.   (let* ((omax (- (buffer-size) (point-max)))
  1143.      (omin (- (buffer-size) (point-min)))
  1144.      (opoint (if (and (> rmail-current-message 0)
  1145.               (= ?D (aref rmail-deleted-vector rmail-current-message)))
  1146.              0 (- (point) (point-min))))
  1147.      (messages-head (cons (aref rmail-message-vector 0) nil))
  1148.      (messages-tail messages-head)
  1149.      ;; Don't make any undo records for the expunging itself.
  1150.      (buffer-undo-list t)
  1151.      (win))
  1152.     (unwind-protect
  1153.     (save-excursion
  1154.       (widen)
  1155.       (goto-char (point-min))
  1156.       (let ((counter 0)
  1157.         (number 1)
  1158.         (total rmail-total-messages)
  1159.         (new-message-number rmail-current-message)
  1160.         (new-summary nil)
  1161.         (buffer-read-only nil)
  1162.         (messages rmail-message-vector)
  1163.         (deleted rmail-deleted-vector)
  1164.         (summary rmail-summary-vector))
  1165.         (setq rmail-total-messages nil
  1166.           rmail-current-message nil
  1167.           rmail-message-vector nil
  1168.           rmail-deleted-vector nil
  1169.           rmail-summary-vector nil)
  1170.         (while (<= number total)
  1171.           (if (= (aref deleted number) ?D)
  1172.           (progn
  1173.             (delete-region
  1174.               (marker-position (aref messages number))
  1175.               (marker-position (aref messages (1+ number))))
  1176.             (move-marker (aref messages number) nil)
  1177.             (if (> new-message-number counter)
  1178.             (setq new-message-number (1- new-message-number))))
  1179.         (setq counter (1+ counter))
  1180.         (setq messages-tail
  1181.               (setcdr messages-tail
  1182.                   (cons (aref messages number) nil)))
  1183.         (setq new-summary
  1184.               (cons (if (= counter number) (aref summary (1- number)))
  1185.                 new-summary)))
  1186.           (if (zerop (% (setq number (1+ number)) 20))
  1187.           (message "Expunging deleted messages...%d" number)))
  1188.         (setq messages-tail
  1189.           (setcdr messages-tail
  1190.               (cons (aref messages number) nil)))
  1191.         (setq rmail-current-message new-message-number
  1192.           rmail-total-messages counter
  1193.           rmail-message-vector (apply 'vector messages-head)
  1194.           rmail-deleted-vector (make-string (1+ counter) ?\ )
  1195.           rmail-summary-vector (vconcat (nreverse new-summary))
  1196.           win t)))
  1197.       (message "Expunging deleted messages...done")
  1198.       (if (not win)
  1199.       (narrow-to-region (- (buffer-size) omin) (- (buffer-size) omax)))
  1200.       (rmail-show-message
  1201.        (if (zerop rmail-current-message) 1 nil))
  1202.       (forward-char opoint))))
  1203.  
  1204. ;;;; *** Rmail Mailing Commands ***
  1205.  
  1206. (defun rmail-mail ()
  1207.   "Send mail in another window.
  1208. While composing the message, use \\[mail-yank-original] to yank the
  1209. original message into it."
  1210.   (interactive)
  1211.   (mail-other-window nil nil nil nil nil (current-buffer)))
  1212.  
  1213. (defun rmail-continue ()
  1214.   "Continue composing outgoing message previously being composed."
  1215.   (interactive)
  1216.   (mail-other-window t))
  1217.  
  1218. (defun rmail-reply (just-sender)
  1219.   "Reply to the current message.
  1220. Normally include CC: to all other recipients of original message;
  1221. prefix argument means ignore them.
  1222. While composing the reply, use \\[mail-yank-original] to yank the
  1223. original message into it."
  1224.   (interactive "P")
  1225.   ;;>> this gets set even if we abort. Can't do anything about it, though.
  1226.   (rmail-set-attribute "answered" t)
  1227.   (rmail-display-labels)
  1228.   (let (from reply-to cc subject date to message-id resent-reply-to)
  1229.     (save-excursion
  1230.       (save-restriction
  1231.     (widen)
  1232.     (goto-char (rmail-msgbeg rmail-current-message))
  1233.     (forward-line 1)
  1234.     (if (= (following-char) ?0)
  1235.         (narrow-to-region
  1236.          (progn (forward-line 2)
  1237.             (point))
  1238.          (progn (search-forward "\n\n" (rmail-msgend rmail-current-message)
  1239.                     'move)
  1240.             (point)))
  1241.       (narrow-to-region (point)
  1242.                 (progn (search-forward "\n*** EOOH ***\n")
  1243.                    (beginning-of-line) (point))))
  1244.     (setq resent-reply-to (mail-fetch-field "resent-reply-to" t)
  1245.           from (mail-fetch-field "from")
  1246.           reply-to (or resent-reply-to
  1247.                (mail-fetch-field "reply-to" nil t)
  1248.                from)
  1249.           cc (cond (just-sender nil)
  1250.                (resent-reply-to (mail-fetch-field "resent-cc" t))
  1251.                (t (mail-fetch-field "cc" nil t)))
  1252.           subject (or (and resent-reply-to
  1253.                    (mail-fetch-field "resent-subject" t))
  1254.               (mail-fetch-field "subject"))
  1255.           date (cond (resent-reply-to
  1256.               (mail-fetch-field "resent-date" t))
  1257.              ((mail-fetch-field "date")))
  1258.           to (cond (resent-reply-to
  1259.             (mail-fetch-field "resent-to" t))
  1260.                ((mail-fetch-field "to" nil t))
  1261.                ;((mail-fetch-field "apparently-to")) ack gag barf
  1262.                (t ""))
  1263.           message-id (cond (resent-reply-to
  1264.                 (mail-fetch-field "resent-message-id" t))
  1265.                    ((mail-fetch-field "message-id"))))))
  1266.     (and subject
  1267.      (string-match "\\`Re: " subject)
  1268.      (setq subject (substring subject 4)))
  1269.     (mail-other-window nil
  1270.       (mail-strip-quoted-names reply-to)
  1271.       subject
  1272.       (rmail-make-in-reply-to-field from date message-id)
  1273.       (if just-sender
  1274.       nil
  1275.     (let* ((cc-list (rmail-dont-reply-to
  1276.               (mail-strip-quoted-names
  1277.                 (if (null cc) to (concat to ", " cc))))))
  1278.       (if (string= cc-list "") nil cc-list)))
  1279.       (current-buffer))))
  1280.  
  1281. (defun rmail-make-in-reply-to-field (from date message-id)
  1282.   (if mail-use-rfc822 (require 'rfc822))
  1283.   (let (field)
  1284.     (if (and mail-use-rfc822 from)
  1285.     (let ((tem (car (rfc822-addresses from))))
  1286.       (and message-id
  1287.            (setq field (if (string-match
  1288.                  (regexp-quote
  1289.                    (if (string-match "@[^@]*\\'" tem)
  1290.                        (substring tem
  1291.                           0 (match-beginning 0))
  1292.                        tem))
  1293.                  message-id)
  1294.                    message-id
  1295.                    (concat message-id " \"" tem "\""))
  1296.              message-id nil date nil))
  1297.       (or field
  1298.           (setq field (prin1-to-string tem))))
  1299. ;    (if message-id
  1300. ;        (setq field message-id message-id nil date nil)
  1301. ;        (setq field (car (rfc882-addresses from))))
  1302.     )
  1303.     (or field
  1304.     (not from)
  1305.     ;; Compute the sender for the in-reply-to; prefer full name.
  1306.     (let* ((stop-pos (string-match "  *at \\|  *@ \\|  *<" from))
  1307.            (start-pos (if stop-pos 0
  1308.                 ;;>> this loses on nested ()'s
  1309.                 (let ((pos (string-match " *(" from)))
  1310.                   (if (not pos) nil
  1311.                 (setq stop-pos (string-match ")" from pos))
  1312.                 (if (zerop pos) 0 (+ 2 pos)))))))
  1313.       (setq field (if stop-pos
  1314.               (substring from start-pos stop-pos)
  1315.               from))))
  1316.     (if date (setq field (concat field "'s message of " date)))
  1317.     (if message-id (setq field (concat field " " message-id)))
  1318.     field))
  1319.  
  1320. (defun rmail-forward ()
  1321.   "Forward the current message to another user."
  1322.   (interactive)
  1323.   ;;>> this gets set even if we abort. Can't do anything about it, though.
  1324.   (rmail-set-attribute "forwarded" t)
  1325.   (let ((forward-buffer (current-buffer))
  1326.     (subject (concat "["
  1327.              (mail-strip-quoted-names (mail-fetch-field "From"))
  1328.              ": " (or (mail-fetch-field "Subject") "") "]")))
  1329.     ;; If only one window, use it for the mail buffer.
  1330.     ;; Otherwise, use another window for the mail buffer
  1331.     ;; so that the Rmail buffer remains visible
  1332.     ;; and sending the mail will get back to it.
  1333.     (if (if (one-window-p t)
  1334.         (mail nil nil subject)
  1335.       (mail-other-window nil nil subject))
  1336.     (save-excursion
  1337.       (goto-char (point-max))
  1338.       (forward-line 1)
  1339.       (insert-buffer forward-buffer)))))
  1340.  
  1341. ;;;; *** Rmail Specify Inbox Files ***
  1342.  
  1343. (autoload 'set-rmail-inbox-list "rmailmsc"
  1344.   "Set the inbox list of the current RMAIL file to FILE-NAME.
  1345. This may be a list of file names separated by commas.
  1346. If FILE-NAME is empty, remove any inbox list."
  1347.   t)
  1348.  
  1349. ;;;; *** Rmail Commands for Labels ***
  1350.  
  1351. (autoload 'rmail-add-label "rmailkwd"
  1352.   "Add LABEL to labels associated with current RMAIL message.
  1353. Completion is performed over known labels when reading."
  1354.   t)
  1355.  
  1356. (autoload 'rmail-kill-label "rmailkwd"
  1357.   "Remove LABEL from labels associated with current RMAIL message.
  1358. Completion is performed over known labels when reading."
  1359.   t)
  1360.  
  1361. (autoload 'rmail-next-labeled-message "rmailkwd"
  1362.   "Show next message with LABEL.  Defaults to last label used.
  1363. With prefix argument N moves forward N messages with this label."
  1364.   t)
  1365.  
  1366. (autoload 'rmail-previous-labeled-message "rmailkwd"
  1367.   "Show previous message with LABEL.  Defaults to last label used.
  1368. With prefix argument N moves backward N messages with this label."
  1369.   t)
  1370.  
  1371. ;;;; *** Rmail Edit Mode ***
  1372.  
  1373. (autoload 'rmail-edit-current-message "rmailedit"
  1374.   "Edit the contents of the current message"
  1375.   t)
  1376.  
  1377. ;;;; *** Rmail Summary Mode ***
  1378.  
  1379. (autoload 'rmail-summary "rmailsum"
  1380.   "Display a summary of all messages, one line per message."
  1381.   t)
  1382.  
  1383. (autoload 'rmail-summary-by-labels "rmailsum"
  1384.   "Display a summary of all messages with one or more LABELS.
  1385. LABELS should be a string containing the desired labels, separated by commas."
  1386.   t)
  1387.  
  1388. (autoload 'rmail-summary-by-recipients "rmailsum"
  1389.   "Display a summary of all messages with the given RECIPIENTS.
  1390. Normally checks the To, From and Cc fields of headers;
  1391. but if PRIMARY-ONLY is non-nil (prefix arg given),
  1392.  only look in the To and From fields.
  1393. RECIPIENTS is a string of names separated by commas."
  1394.   t)
  1395.  
  1396. ;;;; *** Rmail output messages to files ***
  1397.  
  1398. (autoload 'rmail-output-to-rmail-file "rmailout"
  1399.   "Append the current message to an Rmail file named FILE-NAME.
  1400. If the file does not exist, ask if it should be created.
  1401. If file is being visited, the message is appended to the Emacs
  1402. buffer visiting that file."
  1403.   t)
  1404.  
  1405. (autoload 'rmail-output "rmailout"
  1406.   "Append this message to Unix mail file named FILE-NAME."
  1407.   t)
  1408.  
  1409. ;;;; *** Rmail undigestification ***
  1410.  
  1411. (autoload 'undigestify-rmail-message "undigest"
  1412.   "Break up a digest message into its constituent messages.
  1413. Leaves original message, deleted, before the undigestified messages."
  1414.   t)
  1415.